/**
 * mvc pattern - command
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2009. 11. 19 
*/
package com.cmiscm.mvc
{
	import __AS3__.vec.Vector;
	
	/**
     * MVC Command - Singleton Pattern.
     * 
     * @example
     * <listing>
     * // Add Command
     * Controller.getInstance.addCommand(MenuCommand.NAME, new MenuCommand());
     * 
     * // Get Command
     * var menu:MenuCommand = Controller.getInstance.getCommand(MenuCommand.NAME) as MenuCommand;
     * 
     * // Remove Command
     * Controller.getInstance.removeCommand(MenuCommand.NAME);
     * </listing>
     * 
     * @see com.cmiscm.mvc.IController IController
     */	
	public class Controller extends Object
	{
		private var _commandVector:Vector.<IController> = new Vector.<IController>();
        private var _nameVector:Vector.<String> = new Vector.<String>();
        
        private const NO_FOUND_MSG:String = "Command name is no found";
        private const ALREADY_MSG:String = "Command Name is already in use";
        private const SINGLETON_MSG:String = "Singleton already constructed";        
        private static var _instance:Controller;
        
		public function Controller()
        {         
			super(); 
            if (_instance != null) throw Error(SINGLETON_MSG);
            _instance = this;
        }
        public static function get getInstance():Controller
        {
            if (_instance == null) _instance = new Controller();
            return _instance;
        }
		
		/**
         * Get Command 
         * <p>
         * return null in case not find Command
         * </p>
         * @param $name Command Name
         * @return 
         * 
         */ 	
        public function getCommand($name:String):IController
        {
            var index:int = _nameVector.indexOf($name);
            if (index == -1) {
               return null;
            } else {
            	return _commandVector[index];
            }
        }
        
		/**
		 * Add Command  
		 * @param $name Command Name
		 * @param $command Command implements <code>IController</code>
		 * 
		 */       
		public function addCommand($name:String, $command:IController):void
        {
            var index:int = _nameVector.indexOf($name);
            if (index == -1) {
                _commandVector.push($command);
                _nameVector.push($name);
            } else {
                throw Error(ALREADY_MSG);
            }
        }
        
        /**
         * Remove Command  
         * @param $name Command Name
         * 
         */        
        public function removeCommand($name:String):void
        {
            var index:int = _nameVector.indexOf($name);
            if (index == -1) {
                throw Error(NO_FOUND_MSG);
            } else {
            	_commandVector[index] = null;
	            _nameVector[index] = null;
	            _commandVector.splice(index, 1);
	            _nameVector.splice(index, 1);
            }
        }
        
	}
}